import { AsyncStorage } from 'react-native';
import Request from "../utils/Request";
const STORAGE_KEY = "STORAGE_KEY_balance";
export default {
    namespace: 'balance',
    state: {
        loading: false,
        incomeList: [],
        withDrawList: [],
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data))
            });
        },
        *fetchInclomeList({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(Request.get, Request.getApi().incomeList, Object.assign({ store_Id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: { incomeList: data.data }
                });
            }
        },
        *fetchWithDrawList({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(Request.get, Request.getApi().withdrawList, Object.assign({ store_Id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: { withDrawList: data.data }
                });
            }
        }
    },
    subscriptions: {
        setup({ dispatch }) {
        }
    },
};
